import {Reducer} from 'redux';
import {UserState, UserActionTypes} from './types';

import * as actions from './actions';
import {ActionType} from 'typesafe-actions';
export type TUserAction = ActionType<typeof actions>;

const initialState: UserState = {
  phone: null,
  isLogged: false,
  personalVerifying: false,
  profession_status: false,
  nickname: null,
};

const reducer: Reducer<UserState, TUserAction> = (
  state = initialState,
  action,
) => {
  switch (action.type) {
    case UserActionTypes.DID_LOGIN:
      return {...state, isLogged: true};
    case UserActionTypes.WILL_LOGOUT:
      return {...initialState, phone: state.phone};
    case UserActionTypes.UPDATE_USER_INFO:
      return {...state, ...action.payload};
    case UserActionTypes.FETCH_USER_SUCCESS:
      const {
        nickname,
        avatar,
        // identity_status,
        // profession_status,
        menu,
        transaction,
        introducer,
        user_tag,
        banner,
      } = action.payload;

      return {
        ...state,
        user_tag,
        avatar,
        nickname,
        introducer,
        personalVerifying: false,
        profession_status: false,
        menuControl: menu,
        transactionInfo: transaction,
        banner,
      };
    default:
      return state;
  }
};

export default reducer;
